
--#include "..\..\Activity\Answer\AnswerCommon.lua" once

local npcId = 135

NpcDialog[npcId] = {}
local t = NpcDialog[npcId]


t.main = function(sysarg)
	local useCount = GetTodayAnswerTime(sysarg)
	local lastCount = AnswerConfig.CoinCount
	if useCount > 0 then
		lastCount = lastCount - useCount
	end
	local dlgStr = string.format(Lang.ScriptTips.ans001, useCount, lastCount)
	--dlgStr = string.format("%s<#BL%s/@NoticeFreeAnswer>", dlgStr, Lang.ScriptTips.ans018)
	dlgStr = string.format("%s<#BL%s/@JoinFreeAnswer>", dlgStr, Lang.ScriptTips.ans019)
	--dlgStr = string.format("%s<#BL%s/@DoBeginFreeAnswer>", dlgStr, Lang.ScriptTips.ans020)
	if lastCount > 0 then
		dlgStr = string.format("%s<#BL%s/@NoticeCoinAnswer>", dlgStr, Lang.ScriptTips.ans002)
	end	
	dlgStr = string.format("%s<#BL%s/@ViewRanking>", dlgStr, Lang.ScriptTips.ans003)
	dlgStr = string.format("%s<#BL%s/@ViewLastRank>", dlgStr, Lang.ScriptTips.ans014)
	dlgStr = string.format("%s<#BL%s/@AnswerInfo>", dlgStr, Lang.ScriptTips.ans004)
	dlgStr = string.format("%s<#BL%s/@CloseWnd>", dlgStr, Lang.ScriptTips.x00079)
	return dlgStr
end

--提示备用货币答题
t.NoticeCoinAnswer = function(sysarg)
	NoticeCoinAnswer(sysarg)	--AnswerCommon的NoticeCoinAnswer()
	--NoticeFreeAnswer()	--测试
	--BeginFreeAnswer()		--测试
	--SendToPositionRand(sysarg, 2, 135)  --测试
end


--查看排行榜
--function ViewRanking( sysarg )
t.ViewRanking = function(sysarg)
	local netPack = DataPack.allocPacket(sysarg, LogicCmd.otherSys.sysId, LogicCmd.otherSys.sub.sOpenRank)  --申请一个数据包
	if netPack then
		DataPack.writeString(netPack, Lang.Rank.AnswerRank)
		DataPack.flush(netPack)
	end
end

--function AnswerInfo( sysarg )
t.AnswerInfo = function(sysarg)
	return Lang.ScriptTips.ans008 .. Lang.ScriptTips.back001
end

--function CloseWnd( sysarg )
t.CloseWnd = function(sysarg)
	Actor.closeNPCDialog(sysarg)
end

--提示准备免费答题（ScriptTimer）
function NoticeFreeAnswerOld( )
	System.broadcastTipmsg(Lang.ScriptTips.ans007, ttScreenCenter)
end

t.NoticeFreeAnswer = function(sysarg)
	NoticeFreeAnswer()
end


--提示准备免费答题（ScriptTimer）
--弹出消息框
function NoticeFreeAnswer()
	print("AnswerMain.NoticeFreeAnswer()")
	--GetSysAnswerList()
	local sysVar = System.getDyanmicVar()
	sysVar.DoAnswer = 1		--开启答题
	sysVar.DoAnswerTime = System.getCurrMiniTime()	--短时间，从2010年1月1日0点开始的
	local playerList = LuaHelp.getAllActorList()  --获取在线玩家对象列表
	for k, plyPtr in ipairs(playerList or {}) do
		local myLevel = Actor.getIntProperty(plyPtr, PROP_CREATURE_LEVEL)
		if myLevel >= AnswerConfig.FreeMinLevel then
			local dVar = Actor.getDyanmicVar(plyPtr)
			dVar.FreeAnswerFlag = 0
			SendAnswerPack(plyPtr, SC_CanFreeAnswer, SubType_Free, 0, 0)
			Actor.setWorkDayMask(plyPtr, 0, 1, true)	--设置当天秒活动完成
		end
	end
end

--进入答题场景
t.JoinFreeAnswer = function(sysarg)
	EnterAnswerScene(sysarg)
end


--开始免费答题（按钮）
t.DoBeginFreeAnswer = function(sysarg)
	DoBeginFreeAnswer()
end


--开始免费答题(ScriptTimer.lua)
function DoBeginFreeAnswer()
	BeginFreeAnswer()
end

--function ViewLastRank( sysarg )
t.ViewLastRank = function(sysarg)
	local sysVar = System.getStaticVar()
	local size = AnswerConfig.TopCount - 1
	local dlgStr = Lang.ScriptTips.ans015
	local myAward = -1
	local myId = Actor.getIntProperty( sysarg, PROP_ENTITY_ID )
	
	local ranking = Ranking.getRanking( Lang.Rank.LastWeekAnswer )
	if not ranking then
		return
	end
	for i = 0, size do
		local item = Ranking.getItemFromIndex( ranking, i)
		if item then
			local point = Ranking.getPoint(item) or 0
			local name = Ranking.getSub(item, 0) or ""
			local reward = Ranking.getSub(item, 1)		
			local entityId = Ranking.getId(item) or 0
			
			dlgStr = string.format("%s%d,\t%s,\t%d\n", dlgStr, i + 1, name, point)
			if myId == entityId and reward == '0' and point >= AnswerConfig.MinAwardPoint then
				myAward = i
			end
		end
	end
	if myAward >= 0 then
		dlgStr = string.format("%s<#BL%s/@GetAnswerAward,%d>", dlgStr, Lang.Misc.m000431, myAward)
	end	
	return dlgStr .. Lang.ScriptTips.back001
end

--领取答题奖励
function GetAnswerAward( sysarg, awardIdx )
	awardIdx = tonumber(awardIdx)
	local myId = Actor.getIntProperty( sysarg, PROP_ENTITY_ID )
	local ranking = Ranking.getRanking( Lang.Rank.LastWeekAnswer )
	if ranking then
		local item = Ranking.getItemFromIndex( ranking, awardIdx)
		if item then
			--local name = Ranking.getSub(item, 0)
			local entityId = Ranking.getId(item) or 0
			
			local point = Ranking.getPoint(item)
			local reward = Ranking.getSub(item, 1)
			if entityId == myId and reward == "0" and point >= AnswerConfig.MinAwardPoint then
				local newIdx = awardIdx + 1
				if newIdx > 4 and newIdx <= AnswerConfig.TopCount then
					newIdx = 4
				end
				local awardTab = AnswerConfig.AnswerAward[newIdx]
				if awardTab then
					Actor.giveAward(sysarg, awardTab.type,awardTab.id,awardTab.count,awardTab.quality,awardTab.strong,awardTab.bind,0,0,"getAnswerAward")
					Ranking.setSub(item, 1, "1")
				end
			end
		end
	end
end

function InitDialog( sysarg )
	InitRanking( sysarg )
	InitPaTaRanking()
	InitLastWeekRank()
	InitOldPlayerBackRank()
end

function InitOldPlayerBackRank()
	local rankName = Lang.Rank.oldPlayerBack 
	local ranking = Ranking.getRanking( rankName )
	if not ranking then
		ranking = Ranking.add( rankName, 10 )
		if ranking then			
			if not Ranking.load(ranking, rankName) then
				Ranking.addColumn(ranking, "name")
				Ranking.addColumn(ranking, "count")
			end
			Ranking.addRef( ranking )
		end
		--Ranking.save(ranking, rankName)		
	end
end

function FinDialog(sysarg)
	local lastRanking = Ranking.getRanking( Lang.Rank.LastWeekAnswer )
	if lastRanking then		
		Ranking.save(lastRanking, Lang.Rank.LastWeekAnswer)		
	end

	local anwRanking = Ranking.getRanking( Lang.Rank.AnswerRank )
	if anwRanking then
		Ranking.save(anwRanking, Lang.Rank.AnswerRank)
	end

	local fubenRanking = Ranking.getRanking( Lang.Rank.FubenRank )
	if fubenRanking then
		Ranking.save(fubenRanking, Lang.Rank.FubenRank )
	end
end

--table.insert(MainFnTable, Answer_Main)
table.insert(InitFnTable, InitDialog)
table.insert(FinaFnTable, FinDialog)